package com.example.java_chatroom.config;

import com.example.java_chatroom.api.TestWebSocketAPI;
import com.example.java_chatroom.api.WebSocketAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * @author Q
 * @create 2025-07-08-8:17
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    TestWebSocketAPI testWebSocketAPI;

    @Autowired
    WebSocketAPI webSocketAPI;
    /**
     * 将创建好的handler(TestWebSocketAPI)类注册到具体的路径上
     * 当浏览器访问"/test"时，就会调用TestWebSocketAPI里面的方法
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        registry.addHandler(testWebSocketAPI,"/test");
        registry.addHandler(webSocketAPI,"/WebSocketMessage")
                //注册拦截器，从而使用户给HttpSession的键值对复制一份给WebSocketSession
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
